<?php
class AloCodeComponent extends Object
{
    function generateCustomerCode()
    {
        $customer_code = 'KH00000';
        $customerModel = ClassRegistry::init('Customer');
        $last_customer = $customerModel->find('first', array('order' => 'Customer.id DESC'));
        $last_code = str_replace('KH', '', $last_customer['Customer']['code']);
        $new_code = sprintf('%05d', $last_code + 1);
        $customer_code = 'KH' . $new_code;
        return $customer_code;
    }

    function generateBookingCode()
    {
        $today = date('dmy', time());
        $booking_code = 'ALB' . $today;
        $bookingModel = ClassRegistry::init('Booking');
        $last_booking = $bookingModel->find('first', array('conditions' => array('alo_booking_code LIKE' => '%' . $today . '%'), 'order' => 'Booking.id DESC'));
        $booking_count = '1';
        if (!empty($last_booking['Booking']['alo_booking_code']))
        {
            $last_code_parts = explode('-', $last_booking['Booking']['alo_booking_code']);
            if (!empty($last_code_parts[1]))
            {
                $booking_count = $last_code_parts[1] + 1;
            }
        }

        $booking_code = $booking_code . '-' . $booking_count;
        return $booking_code;
    }
}
?>
